#include "ObliqueTile.h"
#include "ObliqueNodeVisitor.h"

namespace OCPP
{
	using namespace Cesium;
	ObliqueTile::ObliqueTile(std::shared_ptr<ObliqueConfig> config, String tileID)
		: _config(config)
		, tileID(tileID)
	{
		_statistic = std::make_shared<ObliqueTileStatistic>();
	}

	void ObliqueTile::read(String fileName)
	{
		osg::ref_ptr<osg::Node> node = osgDB::readNodeFile(fileName);
		if (node.valid())
		{
			ObliqueNodeVisitor visitor(_config, tileID, _statistic);
			visitor.nodeInfo.fileName = "/" + tileID + ".osgb";
			node->accept(visitor);
			nodeInfo = visitor.nodeInfo;
		}
	}

	void ObliqueTile::fromJson(Json::Value& value)
	{
		_statistic->fromJson(value);
	}

	void ObliqueTile::readNodeInfo(String fileName)
	{
		Json::Value value;
		Json::Reader reader;
		std::ifstream ifs(fileName);
		if (!ifs || !reader.parse(ifs, value))
		{
			return;
		}
		nodeInfo.fromJson(value);
	}

	void ObliqueTile::statistic()
	{
		nodeInfo.statistic(_statistic.get());
	}

	Json::Value ObliqueTile::toJson()
	{
		return nodeInfo.toJson();
	}
}